from appium import webdriver
from time import sleep
import os,time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as Ec

class Base():

    def __init__(self):
        #提升权限
        os.system('adb root')

        cap = {
            "platformName": "Android",
            "deviceName": "emulator-5554",
            "noReset": True,
            "appPackage": "com.kongfz.app",
            "appActivity": "com.kongfz.app.home.mvvm.homepage.HomeActivity"
        }

        self.driver = webdriver.Remote("http:127.0.0.1:4723/wd/hub",cap)
        #隐式等待
        self.driver.implicitly_wait(25)
        #强制等待
        sleep(15)


    def find_ele(self,loc):
        if type(loc) == str:
            return self.driver.find_element_by_android_uiautomator(loc)
        else:
            return self.driver.find_element(*loc)

    def find_wait_ele(self,loc):
        return WebDriverWait(self.driver,15).until(Ec.presence_of_element_located(loc))

    def find_ele1(self,loc,key):
        ele = self.find_ele(loc)
        return self.find_ele(key)

    #时间戳
    def jieping(self,filename):
        t = time.strftime("%Y%m%d%H%M%S",time.localtime())
        self.driver.save_screenshot(t+ filename)

    #轻敲
    def qingqiao(self,x,y):
        self.driver.tap([x,y],500)

    #滑动
    def huadong(self,x,y,x1,y1):
        self.driver.swipe(x,y,x1,y1)

    #退出
    def find_quit(self):
        self.driver.quit()
